package atm;

import lovo01.ATM04.Users;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 存款界面
 */
public class SavePanel extends JPanel implements ActionListener {
    private ATM05 atm05;
    private JLabel saveLabel = new JLabel("请输入存款金额:");
    private JTextField saveTextField = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("images/buttonSure.jpg"));
    private JButton offBtn = new JButton(new ImageIcon("images/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,25);


    public SavePanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        saveLabel.setBounds(300,200,200,50);
        saveLabel.setFont(font);
        saveTextField.setBounds(300,260,300,50);
        saveTextField.setFont(font);
        sureBtn.setBounds(320,340,100,50);
        offBtn.setBounds(450,340,100,50);
        sureBtn.addActionListener(this);
        offBtn.addActionListener(this);

        this.add(saveLabel);
        this.add(saveTextField);
        this.add(sureBtn);
        this.add(offBtn);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            Users loginUser = atm05.getLoginUser();
            int money = 0;
            try {
                String money02 = saveTextField.getText();
                money = Integer.parseInt(money02);
            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"输入错误,请重新输入");
                saveTextField.setText(null);
                return;
            }
            loginUser.setMoney(loginUser.getMoney() + money);
            JOptionPane.showMessageDialog(this,"存款成功");
            saveTextField.setText("");
            atm05.showPanel("menu");
        }
        if (e.getSource().equals(offBtn)) {
            saveTextField.setText("");
            atm05.showPanel("menu");
        }
    }
}
